﻿using System;
using System.Windows.Controls;
using Might.Infrastructure.Annotations.Graphics;

namespace Might.GraphicsEditor.GraphicsObjects {
    /// <summary>
    /// Class representation for simple rectangle
    /// </summary>
    public class LeafText : GraphicsComponent {
        private readonly TextBlock textBlock = new TextBlock();

        /// <summary>
        /// Public constructor for simple rectangle
        /// </summary>
        /// <param name="ga">Graphic annotation for this object</param>
        public LeafText(IGraphicsItem ga) {
            this.ga = ga;

            Content = textBlock;

            UpdateShape();

            this.ga.Changed += GaGraphicsPrimitiveChanged;
        }

        protected override void UpdateShape() {
            base.UpdateShape();
            IGraphicsText g = ga as IGraphicsText;
            if (g == null) {
                throw new NullReferenceException("Updating Null anotation");
            }
            Text = g.Text;
            FontSize = g.FontSize;
        }

        // TODO add regexp parsing
        /// <summary>
        /// Text of this element
        /// </summary>
        public string Text {
            get { return textBlock.Text; }
            set {
                if (textBlock.Text != value) {
                    textBlock.Text = value;
                }
            }
        }
    }
}
